package com.yougou.ui.home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yougou.R;
import com.yougou.data.model.Floor;

import java.util.ArrayList;
import java.util.List;

public class FloorAdapter extends RecyclerView.Adapter<FloorAdapter.ViewHolder> {
    
    private List<Floor> mDataList = new ArrayList<>();
    private OnProductClickListener mOnProductClickListener;
    
    public void setData(List<Floor> dataList) {
        if (dataList != null) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }
    
    public void setOnProductClickListener(OnProductClickListener listener) {
        this.mOnProductClickListener = listener;
    }
    
    public interface OnProductClickListener {
        void onProductClick(String query);
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_floor, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Floor floor = mDataList.get(position);
        
        // 设置楼层标题
        holder.tvFloorTitle.setText(floor.getFloorTitle().getName());
        String imageUrl = floor.getFloorTitle().getImageSrc();
        if (imageUrl != null && !imageUrl.trim().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .into(holder.ivFloorTitle);
        } else {
            holder.ivFloorTitle.setImageResource(R.drawable.placeholder_image);
        }
        
        // 设置商品列表
        FloorProductAdapter productAdapter = new FloorProductAdapter(floor.getProductList(), mOnProductClickListener);
        holder.rvProducts.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 3));
        holder.rvProducts.setAdapter(productAdapter);
    }
    
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvFloorTitle;
        ImageView ivFloorTitle;
        RecyclerView rvProducts;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvFloorTitle = itemView.findViewById(R.id.tv_floor_title);
            ivFloorTitle = itemView.findViewById(R.id.iv_floor_title);
            rvProducts = itemView.findViewById(R.id.rv_products);
        }
    }
    
    
    static class FloorProductAdapter extends RecyclerView.Adapter<FloorProductAdapter.ProductViewHolder> {
        
        private final List<Floor.Product> mProductList;
        private final OnProductClickListener mListener;
        
        FloorProductAdapter(List<Floor.Product> productList, OnProductClickListener listener) {
            mProductList = productList != null ? productList : new ArrayList<>();
            mListener = listener;
        }
        
        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_floor_product, parent, false);
            return new ProductViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
            Floor.Product product = mProductList.get(position);
            
            holder.tvProductName.setText(product.getName());
            String imageUrl = product.getImageSrc();
            if (imageUrl != null && !imageUrl.trim().isEmpty()) {
                Glide.with(holder.itemView.getContext())
                        .load(imageUrl)
                        .placeholder(R.drawable.placeholder_image)
                        .error(R.drawable.placeholder_image)
                        .into(holder.ivProduct);
            } else {
                holder.ivProduct.setImageResource(R.drawable.placeholder_image);
            }
            
            // 点击事件 - 从 navigatorUrl 中提取 query 参数
            holder.itemView.setOnClickListener(v -> {
                if (mListener != null) {
                    String query = parseQuery(product.getNavigatorUrl());
                    if (query != null && !query.isEmpty()) {
                        mListener.onProductClick(query);
                    }
                }
            });
        }
        
        
        private String parseQuery(String navigatorUrl) {
            if (navigatorUrl == null || navigatorUrl.isEmpty()) {
                return null;
            }
            try {
                // 查找 query= 参数
                String[] parts = navigatorUrl.split("query=");
                if (parts.length > 1) {
                    // 提取关键词部分（可能后面还有其他参数）
                    String queryPart = parts[1].split("&")[0];
                    return java.net.URLDecoder.decode(queryPart, "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        
        @Override
        public int getItemCount() {
            return mProductList.size();
        }
        
        static class ProductViewHolder extends RecyclerView.ViewHolder {
            ImageView ivProduct;
            TextView tvProductName;
            
            ProductViewHolder(@NonNull View itemView) {
                super(itemView);
                ivProduct = itemView.findViewById(R.id.iv_product);
                tvProductName = itemView.findViewById(R.id.tv_product_name);
            }
        }
    }
}

